package visiopuce.ui;

import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.nebula.widgets.formattedtext.DateFormatter;
import org.eclipse.nebula.widgets.formattedtext.FormattedText;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import visiopuce.ApplicationContexte;
import visiopuce.objets.Analyse;
import visiopuce.objets.CategorieNonConformite;
import visiopuce.objets.NonConformite;
import visiopuce.objets.Personne;
import visiopuce.objets.Prelevement;
import visiopuce.objets.TypeNonConformite;
import visiopuce.service.AnalyseServiceImpl;
import visiopuce.service.PersonneServiceImpl;
import visiopuce.service.PrelevementServiceImpl;
import visiopuce.service.ReferentielServiceImpl;
import visiopuce.ui.providers.AnalyseLabelProvider;
import visiopuce.ui.providers.CategorieNonConformiteLabelProvider;
import visiopuce.ui.providers.PrelevementLabelProvider;
import visiopuce.ui.providers.SousCategorieNonConformiteLabelProvider;
import visiopuce.ui.providers.TypeNonConformiteLabelProvider;

public class NonConformiteDialog extends Dialog {

	private NonConformite nonConf;
	private Composite container;
	private FormattedText dObs;
	private ComboViewer comboType;
	private ComboViewer comboCat;
	private ComboViewer comboSousCat;
	private Combo comboAcceptation;
	private ComboViewer listPrelevement;
	private ComboViewer listAnalyse;

	public NonConformiteDialog(NonConformite nonConf) {
		super(Display.getCurrent().getActiveShell());
		setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
		setBlockOnOpen(true);
		this.nonConf = nonConf;
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Création d'une feuille de travail de puce");
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Personne p = null;
		container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1, false));

		Group gIdent = new Group(container, SWT.BORDER);
		gIdent.setText("Identité");
		gIdent.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		gIdent.setLayout(new GridLayout(4, false));

		Label lblNom = new Label(gIdent, SWT.NONE);
		lblNom.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNom.setText("Nom :");

		Text tNom = new Text(gIdent, SWT.BORDER);
		GridData gd_tNom = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		tNom.setLayoutData(gd_tNom);

		Label lblPreNom = new Label(gIdent, SWT.NONE);
		lblPreNom.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblPreNom.setText("Prénom :");

		Text tPreNom = new Text(gIdent, SWT.BORDER);
		GridData gd_tPreNom = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		tPreNom.setLayoutData(gd_tPreNom);

		if (nonConf.getPersonne() != null) {
			p = PersonneServiceImpl.getInstance().getPersonneById(nonConf.getPersonne().getIdPersonne());
			tNom.setText(p.getNom());
			tPreNom.setText(p.getPrenom());
		}
		tNom.setEnabled(false);
		tPreNom.setEnabled(false);

		final Group gDescription = new Group(container, SWT.BORDER);
		gDescription.setText("Description");
		gDescription.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		gDescription.setLayout(new GridLayout(6, false));

		Label lblType = new Label(gDescription, SWT.NONE);
		lblType.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblType.setText("Type :");

		comboType = new ComboViewer(gDescription, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboType.setContentProvider(ArrayContentProvider.getInstance());
		comboType.setLabelProvider(new TypeNonConformiteLabelProvider());
		comboType.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		comboType.setInput(ReferentielServiceImpl.getInstance().getTypeNonConformite());
		comboType.addSelectionChangedListener(new ISelectionChangedListener() {

			public void selectionChanged(SelectionChangedEvent event) {
				TypeNonConformite typeNonConformite = null;
				ISelection selectiontypeNonConformite = comboType.getSelection();
				if (!selectiontypeNonConformite.isEmpty()) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selectiontypeNonConformite;
					typeNonConformite = (TypeNonConformite) structuredSelection.getFirstElement();
					comboCat.setInput(ReferentielServiceImpl.getInstance().getCategorieNonConfByIdTypeNonConformite(typeNonConformite.getIdTypeNonConformite()));
					comboSousCat.setInput(null);
				} else {
					comboCat.setInput(null);
					comboSousCat.setInput(null);
				}
				gDescription.pack();
			}
		});

		Label lblCat = new Label(gDescription, SWT.NONE);
		lblCat.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblCat.setText("Catégorie :");

		comboCat = new ComboViewer(gDescription, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboCat.setContentProvider(ArrayContentProvider.getInstance());
		comboCat.setLabelProvider(new CategorieNonConformiteLabelProvider());
		comboCat.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		if (nonConf.getCategorie() != null) {
			comboCat.setInput(ReferentielServiceImpl.getInstance().getCategorieNonConfByIdTypeNonConformite(nonConf.getCategorie().getIdCategorieNonConformite()));
			
		} else {
			comboCat.setInput(null);
		
		}
		comboCat.addSelectionChangedListener(new ISelectionChangedListener() {

			public void selectionChanged(SelectionChangedEvent event) {
				CategorieNonConformite catNonConformite = null;
				ISelection selectioncatNonConformite = comboCat.getSelection();
				if (!selectioncatNonConformite.isEmpty()) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selectioncatNonConformite;
					catNonConformite = (CategorieNonConformite) structuredSelection.getFirstElement();
					comboSousCat.setInput(ReferentielServiceImpl.getInstance().getSousCategorieNonConfByIdCategorieNonConformite(catNonConformite.getIdCategorieNonConformite()));
				} else {
					comboSousCat.setInput(null);
				}
				gDescription.pack();
			}
		});

		Label lblSouCat = new Label(gDescription, SWT.NONE);
		lblSouCat.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblSouCat.setText("Sous-catégorie :");

		comboSousCat = new ComboViewer(gDescription, SWT.DROP_DOWN | SWT.READ_ONLY);
		comboSousCat.setContentProvider(ArrayContentProvider.getInstance());
		comboSousCat.setLabelProvider(new SousCategorieNonConformiteLabelProvider());
		comboSousCat.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		if (nonConf.getSousCategorie() != null) {
			comboSousCat.setInput(ReferentielServiceImpl.getInstance().getSousCategorieNonConfByIdCategorieNonConformite(nonConf.getSousCategorie().getIdSousCategorieNonConformite()));
		} else {
			comboSousCat.setInput(null);
		}

		Label lblAcc = new Label(gDescription, SWT.NONE);
		lblAcc.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblAcc.setText("Acceptation :");

		comboAcceptation = new Combo(gDescription, SWT.READ_ONLY);
		comboAcceptation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,5, 1));
		comboAcceptation.setItems(ApplicationContexte.acceptations);

		Label lblComm = new Label(gDescription, SWT.NONE);
		lblComm.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblComm.setText("Commentaire :");

		Text tCommentaire = new Text(gDescription, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_tCommentaire = new GridData(SWT.FILL, SWT.CENTER, false, false, 5, 1);
		gd_tCommentaire.widthHint = 250;
		gd_tCommentaire.heightHint = 96;
		tCommentaire.setLayoutData(gd_tCommentaire);
		if (nonConf.getCommentaire() != null) {
			tCommentaire.setText(nonConf.getCommentaire());
		}

		Label lblDateDeLa = new Label(gDescription, SWT.NONE);
		lblDateDeLa.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblDateDeLa.setText("Date obs :");

		dObs = new FormattedText(gDescription, SWT.BORDER);
		dObs.setFormatter(new DateFormatter("dd/MM/yyyy"));
		GridData gridData_ddObs = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gridData_ddObs.widthHint = 75;
		dObs.getControl().setLayoutData(gridData_ddObs);
		if (nonConf.getDateObservation() != null) {
			dObs.setValue(nonConf.getDateObservation());
		}

		Label lblListPrelev = new Label(gDescription, SWT.NONE);
		lblListPrelev.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblListPrelev.setText("Associé au prélèvement :");

		
		listPrelevement = new ComboViewer(gDescription, SWT.DROP_DOWN | SWT.READ_ONLY);
		listPrelevement.setContentProvider(ArrayContentProvider.getInstance());
		listPrelevement.setLabelProvider(new PrelevementLabelProvider());
		listPrelevement.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		if (p != null) {
			java.util.List<Prelevement> prelevements = PrelevementServiceImpl.getInstance().findAllByidPersonne(p.getIdPersonne());
			listPrelevement.setInput(prelevements);
		} else {
			listPrelevement.setInput(null);
		}
		listPrelevement.addSelectionChangedListener(new ISelectionChangedListener() {

			public void selectionChanged(SelectionChangedEvent event) {
				Prelevement prelevement = null;
				ISelection selectionPrelevement = listPrelevement.getSelection();
				if (!selectionPrelevement.isEmpty()) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selectionPrelevement;
					prelevement = (Prelevement) structuredSelection.getFirstElement();
					List<Analyse> ana= AnalyseServiceImpl.getInstance().findAllByidPrelevement(prelevement.getIdPrelevement());
					ana.add(new Analyse());
					listAnalyse.setInput(ana);
				} else {
					listAnalyse.setInput(null);
				}
				gDescription.pack();
			}
		});

		
		Label lblListAnalyse = new Label(gDescription, SWT.NONE);
		lblListAnalyse.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblListAnalyse.setText("pour l'analyse :");

		
		listAnalyse = new ComboViewer(gDescription, SWT.DROP_DOWN | SWT.READ_ONLY);
		listAnalyse.setContentProvider(ArrayContentProvider.getInstance());
		listAnalyse.setLabelProvider(new AnalyseLabelProvider());
		listAnalyse.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		
		return container;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button bOk = createButton(parent, IDialogConstants.OK_ID, "Créer la non conformité", true);
		bOk.setToolTipText("Créer la non conformité");
		Button bCl = createButton(parent, IDialogConstants.CANCEL_ID, "Annuler", false);
		bCl.setToolTipText("Annuler l'enregistrement");
	}

	@Override
	protected Point getInitialSize() {
		return new Point(1024, 500);
	}

	@Override
	protected void okPressed() {
		if (verifierChamp()) {
			super.okPressed();
		}
	}

	private boolean verifierChamp() {
		if (dObs.getValue() == null) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(), "Champs obligatoire manquant", "Date de l'observation");
			return false;
		}
		if (comboType.getSelection().isEmpty()) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(), "Champs obligatoire manquant", "Type de non conformité");
			return false;
		}
		return true;
	}

}
